package me.leiusual.sharing

import android.content.Context
import androidx.multidex.MultiDex
import cn.bingoogolapple.photopicker.imageloader.BGAImage
import com.mob.MobSDK
import com.tencent.bugly.Bugly
import com.tencent.bugly.beta.Beta
import dagger.android.AndroidInjector
import dagger.android.support.DaggerApplication
import io.reactivex.plugins.RxJavaPlugins
import me.leiusual.sharing.di.DaggerAppComponent
import me.leiusual.sharing.ui.MainActivity
import me.leiusual.sharing.util.tool.BGAGlideImageLoader490
import me.leiusual.sharing.util.tool.Constants
import me.leiusual.sharing.util.tool.LogbackTree
import timber.log.Timber

/**
 * Create by leiusual on 2018/7/15
 */
class SharingApplication : DaggerApplication() {

  override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
    return DaggerAppComponent.factory().create(this)
  }

  override fun attachBaseContext(base: Context?) {
    super.attachBaseContext(base)

    MultiDex.install(this)
  }

  override fun onCreate() {
    super.onCreate()

    Timber.plant(LogbackTree())

    Beta.autoInit = true
    Beta.autoCheckUpgrade = true
    Beta.upgradeCheckPeriod = 60 * 1000
    Beta.initDelay = 5 * 1000
    Beta.showInterruptedStrategy = true
    Beta.canShowUpgradeActs.add(MainActivity::class.java)
    Bugly.init(this, Constants.BUGLY_APP_ID, BuildConfig.DEBUG)

    MobSDK.init(this)

    RxJavaPlugins.setErrorHandler { Timber.w(it) }

    BGAImage.setImageLoader(BGAGlideImageLoader490())
  }

  override fun onLowMemory() {
    super.onLowMemory()

    GlideApp.get(this).onLowMemory()
  }

  override fun onTrimMemory(level: Int) {
    super.onTrimMemory(level)

    GlideApp.get(this).onTrimMemory(level)
  }
}